import subject from "@/config/mars3d";
import mapOptions from "@/config/map.config";
import { monitor } from "@/utils/tool/saveDrawData";
import { closeContextMenu, graphicStoptEditing } from "@/utils/global.function";

import global from "@/utils/global.data.params";
import { loadFon } from "@/utils/load";
import { useStore } from "@/stores/index1";
import { getCoverage } from "@/api/coverage";
import { getAllDrawData } from "@/api/draw";
const store = useStore();

// import { useStoreData } from "@/stores/data";
// const storeData: any = useStoreData();
import req from "@/utils/data.function";
export let map: any = "";
export let graphicLayer: any;
/**
 * @function initMap 加载地图
 * @param key 传入地图唯一key，防止同一页面有两个以上地图导致的id冲突，默认时间戳
 * @author 染指你的唇 2024-1-31 14:40:58
 */
export function initMap(key: number) {
    // 创建三维地球场景

    map = new subject.mars3d.Map(`mars3dContainer${key}`, mapOptions);

    // 地球加载动画
    map.openFlyAnimation({
        // center: { lat: 29.534187, lng: 106.479913, alt: 66666, heading: 0, pitch: -90 },
        // center: mapOptions.,
        callback: () => {
            console.log("地图加载完成^_^");
            store.map = map;
            graphicLayer = new subject.mars3d.layer.GraphicLayer({
                zIndex: 999,
                // hasEdit: true,
            });
            map.addLayer(graphicLayer);
            store.graphicLayer = graphicLayer;
            mapBindingFun(map);

            loadFon();
            // 监听图层对象绘制情况
            monitor(graphicLayer);
            // 获取图层树数据
            // req.coverage();
            setTimeout(() => {
                getCoverage();
                getAllDrawData();
            }, 3000);

            setTimeout(() => {
                // // 安徽省卫星底图
                const tileLayer = new subject.mars3d.layer.XyzLayer({
                    url: "http://localtest.sxfarmer.com:5555/kaizhou/map/{z}/{x}/{y}.png",
                    minimumLevel: 12,
                    maximumLevel: 21,
                    rectangle: { xmin: 108.48996410852341, xmax: 108.49596242309576, ymin: 31.623637119001035, ymax: 31.627198033989142 },
                    flyTo: true,
                });
                map.addLayer(tileLayer);
                // const tileLayer = new subject.mars3d.layer.ImageLayer({
                //     name: "中科大-西区",
                //     url: "http://localtest.sxfarmer.com:5555/kaizhou/map/report/dom_screennail.png",
                //     rectangle: { xmin: 108.4899650845, xmax: 108.4959625628, ymin: 31.6236370315, ymax: 31.6271981018 },
                //     zIndex: 20,
                //     flyTo: true,
                // });
                // map.addLayer(tileLayer);
            }, 1000);
            // getCoverage({
            //     id: store.baseId,
            // }).then((res) => {
            //     // let data = res.data;
            //     // storeData.treeData = data;
            //     // if (!storeData.treeDefaultKey) {
            //     //     storeData.treeDefaultKey = data[0].id;
            //     // }
            // });
        },
    });
}
/**
 * @function mapBindingFun 绑定地图对象事件
 * @param map 地图对象
 * @author 染指你的唇 2024-2-1 10:08:59
 */
function mapBindingFun(map: any) {
    map.on(subject.mars3d.EventType.click, (e: any) => {
        // 点击地图
        e.stopPropagation();
        // 如果右键菜单是打开状态，关闭右键菜单
        closeContextMenu();
        // 标绘结束编辑
        graphicStoptEditing();
    }).on(subject.mars3d.EventType.rightClick, (e: any) => {
        // 右击地图
        e.stopPropagation();
        // console.log(321);
        // 如果右键菜单是打开状态，关闭右键菜单
        closeContextMenu();
    });
}
